﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using AmkeniCrossLoginApplication.Data_Access;
using Amkeni_System.Classes;
using Amkeni_System.Report;

namespace Amkeni_System.Programme
{
    public partial class Submitted_Project_Reports : PDFCreator
    {
        protected void Page_PreInit(object sender, EventArgs e)
        {
            /*if (User.IsInRole("IP"))
            {
                MasterPageFile = "~/MasterPages/Project.Master";
            }
            if (User.IsInRole("Programme"))
            {
                MasterPageFile = "~/MasterPages/Programme.Master";
            }
            if (User.IsInRole("Administrator"))
            {
                MasterPageFile = "~/MasterPages/CO.Master";
            }
            if (User.IsInRole("IP") && User.IsInRole("Programme"))
            {
                MasterPageFile = "~/MasterPages/ProgProject.Master";
            }*/
        }

        Amkeni_DatabaseEntities db = new Amkeni_DatabaseEntities();

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void SubmittedProjectsGridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            //Get the selected row
            int currentRowIndex = Convert.ToInt32(e.CommandArgument);
            GridViewRow row = SubmittedProjectsGridView.Rows[currentRowIndex];

            //Get the Datakeynames for the selected row
            string selIP = this.SubmittedProjectsGridView.DataKeys[currentRowIndex].Values["ip_id"].ToString();
            string selYr = this.SubmittedProjectsGridView.DataKeys[currentRowIndex].Values["year_id"].ToString();
            string selRPRD = this.SubmittedProjectsGridView.DataKeys[currentRowIndex].Values["report_period_id"].ToString();
            string selRFP = this.SubmittedProjectsGridView.DataKeys[currentRowIndex].Values["RFP_id"].ToString();

            int ip = int.Parse(selIP);
            int reportp = int.Parse(selRPRD);
            int year = int.Parse(selYr);
            int rfp = int.Parse(selRFP);

            if (e.CommandName == "pdf")
            {
                if (!User.IsInRole("Administrator"))
                {
                    var URegion = (from u in db.users where u.email == User.Identity.Name select u.region).FirstOrDefault();
                    if (URegion == null)
                    {
                        Error.Text = "You cannot access this document";
                        Error.Visible = true;
                        return;
                    }

                    string[] thematicArea = URegion.Split(new Char[] { ',' });

                    foreach (var area in thematicArea)
                    {
                        int themArea = int.Parse(area);

                        int ProjsUnderProgs = (from p in db.implementing_partner 
                                               where p.programme_outcome.outcome_id == themArea 
                                               select p.ip_id).FirstOrDefault();
                        //foreach (var selProjs in ProjsUnderProgs)
                        //{
                            if (themArea == ProjsUnderProgs)
                            {
                                //New PDF Doc
                                CreatePDF(ip, reportp, year, rfp);
                            }
                            else
                            {
                                Error.Text = "You cannot access this document";
                                Error.Visible = true;
                            }
                        //}
                    }
                }
                else
                {
                    //New PDF Doc
                    CreatePDF(ip, reportp, year, rfp);
                }
            }
            else if (e.CommandName == "view")
            {
                if (!User.IsInRole("Administrator"))
                {
                    var URegion = (from u in db.users where u.email == User.Identity.Name select u.region).FirstOrDefault();
                    if (URegion == null)
                    {
                        Error.Text = "You cannot access this report";
                        Error.Visible = true;
                        return;
                    }

                    string[] theRegion = URegion.Split(new Char[] { ',' });

                    foreach (var _region in theRegion)
                    {
                        if (_region != "")
                        {
                            int region = int.Parse(_region);

                            int IPsUnderRegions = (from p in db.implementing_partner
                                                   where p.region.region_id == region
                                                   select p.ip_id).FirstOrDefault();

                            //foreach (var selProjs in ProjsUnderProgs)
                            //{
                            if (region == IPsUnderRegions)
                            {
                                Session["Sub_Project"] = ip;
                                Session["Sub_ReportPeriod"] = reportp;
                                Session["Sub_Year"] = year;
                                Session["Sub_Call"] = rfp;

                                Response.Redirect("~/Amkeni_System/Programme/Submitted_Report/");
                            }
                            else
                            {
                                Error.Text = "You cannot access this report";
                                Error.Visible = true;
                            }
                            //}
                        }
                    }
                }
                else
                {
                    //ProjectLabel.Text = "Project : " + proj;
                    //YearLabel.Text = "Year : " + yr;
                    //ReportPeriodLabel.Text = "Report period : " + reprt_per;

                    Session["Sub_Project"] = ip;
                    Session["Sub_ReportPeriod"] = reportp;
                    Session["Sub_Year"] = year;
                    Session["Sub_Call"] = rfp;

                    Response.Redirect("~/Amkeni_System/Programme/Submitted_Report/");
                }
            }
        }
    }
}